comp2013group7fandomcom-20200214-history
User blog:Kmrugala/WiFi Library and Flask Blueprints
We decided that we need to finish and finalise the hardware, since some of us (including me) are leaving for Easter. Therefore this week I was trying to fix and expand the Wi-Fi library for everyone to use. At the end of last week I had something basic working, however there were some issues with it, and it took me a lot of time to fix them. For some reason after sending the first request it parsed everything properly and the right command was extracted from the URL. However on the second try the Wi-Fi library "skips" the first line of the request and goes to the next one. On the third try it "skips" two and so on. In the end I managed to establish that this is because I stop reading the response once the first line is read and parsed. To fix that I added an additional loop which just reads through the rest of the response before continuing onwards. That worked and solved the issue, though I am not sure if that's the best way to go about it. The other thing I was trying to achieve was to make the Arduino send requests to the server, so that our code can be truly event driven. Because of the structure of the code and the middle layers that should handle all connection to the hardware, I did not want to include the URL in the main Flask Server file, here Flask Blueprints really came in handy. That way I could send it to the right URL and the right class would receive it. On the Arduino sending a request was fairly easy to do because of the libraries that are already there to help out and the amount of examples showing how to do it is tremendous. That I managed to get working fine, however I found a problem when I was trying to run a Server on Arduino and send requests to a Server from Arduino at the same time. I still cannot fix it. Right now the Arduino behaves like it was multithreaded (although it is not) and even if it is in a middle of sending a response to the Flask Server (or rather the blueprint in Arduino Middle Layer), and someone posts a request it would stop sending and start receiving. The same thing happens the other way round. I do not understand why it happens, both things are executed one after the other and both have a while loop in them, which should not exit before the entire response is sent or read. The fact that I am writing the library in a language that I am not familiar with also slows things down and gives me additional errors to worry about. C++ is similar to C, but it has concepts that are different, and the fact that I do not know them well enough means that I have additional errors that for a C++ programmer are obvious, but are not for me. But don’t get me wrong in any way. It is a very good learning opportunity. Until now I did not dealt much with hardware as such, and now I can see how many small details I have to account for, which are not present in higher level designs. Like the change in voltages which sometimes “just happen” and the code need to account for them. I also learn a bit of C++, which introduces me to a new programming language, and as such expands my horizons. Category:Blog posts